package com.up360.rapidcalc.common.rankinglist
{
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.ColorTransform;
	import flash.net.URLVariables;
	
	
	/**
	 * Author:
	 * Date: May 2, 2013 1:48:08 PM
	 * Synopsis:
	 */
	public class RankingList extends Sprite
	{
        /**
         *胜利主体颜色 
         */
        public static const SUCCESS_COLOR:int=0;
        /**
         *失败主体颜色 
         */
        public static const FAILURE_COLOR:int=1;
        /**
         *表示一切都搞定了
         */
        public static const FINISHED:String="finished";
        /**
         *学生UI元件命名 
         */
        private static var stuUIName:String="mcStudent";
        /**
         *路径 
         */
        private var _url:String;
        /**
         *需要post的参数 ,目前只需要一个关卡参数
         */
        private var _vals:URLVariables;
        /**
         *背景主体颜色类型 
         */
        private var _colorType:int;
        /**
         *学生dao 
         */
        private var _stuDao:StudentDao;
        /**
         *学生列表 
         */
        private var _stuList:Array;
        /**
         *ui 
         */
        private var _ui:RankingUI;
        
		public function RankingList(url:String,vals:URLVariables,colorType:int=0)
		{
			super();
            _url=url;
            _vals=vals;
            _colorType=colorType;
            //
            init();
		}
		
        /**
         *设置排行榜主体颜色 
         * @param trans
         * 
         */
        public function setBackground(red:uint,green:uint,blue:uint):void
        {
            var b_trans:ColorTransform=new ColorTransform(1,1,1,1,red,green,blue);
            _ui.mcBorder.transform.colorTransform=b_trans;
            _ui.mcTextStroke.transform.colorTransform=b_trans;
        }
        
        /**
         *初始化 
         */
        public function init():void
        {
            clear();
            //写死
            _ui=new RankingUI();
            addChild(_ui);
            //设置主体颜色
            switch(_colorType)
            {
                case SUCCESS_COLOR:
                    setBackground(0x10,0x96,0x61);
                    break;
                case FAILURE_COLOR:
                    setBackground(0x5d,0x5e,0x60);
                    break;
                default:
                    break;
            }
            //
            _stuDao=new StudentDao();
            _stuDao.access(_url,_vals,accessComp,accessError);
        }
        
        private function accessComp(data:Object):void
        {
            var b_list:Array=data.list as Array;
            checkStuList();
            for each(var obj:Object in b_list)
            {
                _stuList.push(_stuDao.newStudent(obj));
            }
//            trace(_stuList);
            //将学生数据显示在ui的对应位置上
            showStuList();
        }
        /**
         *检测学生列表 
         *若不为null,则清空.
         *若为null,则new出来.
         */
        private function checkStuList():void
        {
            if(_stuList)
            {
                for each(var obj:Object in _stuList)
                {
                    obj=null;
                }
                _stuList.splice(0,_stuList.length);
            }
            else
            {
                _stuList=new Array();
            }
        }
        
        /**
         *在ui的对应位置上显示学生信息 
         */
        private function showStuList():void
        {
            var b_size:int=_stuList.length;
            for(var i:int=0;i<b_size;i++)
            {
                applyStuUI(_stuList[i] as Student,i);
            }
            for(b_size;b_size<5;b_size++)
            {
                hideStuUI(b_size);
            }
            //通知外部,排行榜已经搞定了
            dispatchEvent(new Event(FINISHED));
        }
        
        private function applyStuUI(stu:Student, i:int):void
        {
            var b_stuUI:MovieClip=_ui[stuUIName+i];
            b_stuUI.txtUserName.text=stu.userName;
            b_stuUI.txtSchool.text=stu.school;
            b_stuUI.txtScore.text=stu.score;
            //头像显示
            _stuDao.accessPortrait(stu.portrait,b_stuUI.mcPortrait);
        }
        
        /**
         *hide no apply student ui; 
         * @param b_size
         */
        private function hideStuUI(i:int):void
        {
            var b_stuUI:MovieClip=_ui[stuUIName+i];
            b_stuUI.visible=false;
        }
        
        private function accessError():void
        {
            // TODO Auto Generated method stub
        }
        /**
         *清空内存 
         */
        public function clear():void
        {
            
        }
	}
}